<?php defined('SYSPATH') or die('No direct script access.');

	class Controller_RawSignalWizard extends Controller{
		
		//Carga el wizard component
		public function action_index(){
			$this->request->response = View::factory("rawsignalwizard/wizard")->render(); 
		}
		
		public function action_firststep(){
			$uploadView = View::factory("rawsignalwizard/step1-upload");
			$this->request->response = $uploadView->render();
		}
		
		public function action_checkFile(){
			$fileArray = array();
			foreach ($_POST as $key => $value) {
				if ($key != 'folder') {
					if (file_exists($_SERVER['DOCUMENT_ROOT'] . $_POST['folder'] . '/' . $value)) {
						$fileArray[$key] = $value;
					}
				}
			}
			$this->request->response = json_encode($fileArray);
		}
		
		public function action_uploadSignal(){
			if (!empty($_FILES)) {
				$tempFile = $_FILES['Filedata']['tmp_name'];
				$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
				$targetFile =  str_replace('//','/',$targetPath).$_FILES['Filedata']['name'];

				//Escribe en el log
				Kohana::$log->add('FirePHP::LOG', $targetFile)->write();
				
				//Mueve el archivo de la carpeta temporal a la carpeta target
				move_uploaded_file($tempFile,$targetFile);
					
				
				//Almacena en la base de datos - en realidad debiera de almacenar en una variable de sesion
				$reader = Model::factory("filesignalreader");
				$reader->read($targetFile);
				
				$data = array();
				$data["signal"] = $reader->getSegment()->serialize();
				$data["filename"] = $_FILES['Filedata']['name'];
				$data["sampling_period"] = 1;
				
				Helper_Service::factory("rawsignal")->save(0,$data);
							
			}
			$this->request->response = '1';
		}
		
		
		public function action_secondstep(){
			$this->request->response = "Este es el segundo paso";
			
		}
		
		public function action_thirdstep(){
			$this->request->response = "Este es el tercer paso";
		}
		
		
		

	}
?>